﻿#region Namespace Inclusions
using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Threading;
using System.Timers;
using SCI_actuator.Properties;
#endregion

namespace SCI_actuator
{
    public partial class posi_limit : Form
    {
        private menu_control frmain;
        private xmlrw scixml;
        public posi_limit(menu_control fr)
        {
            InitializeComponent();
            this.frmain = fr;
            scixml = new xmlrw();
            txtMAXH.Text = Settings.Default.MaxPosiH;
            txtMAXL.Text = Settings.Default.MaxPosiL;
            txtMINH.Text = Settings.Default.MinPosiH;
            txtMINL.Text = Settings.Default.MinPosiL;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(scixml.Read_ServoID() + "0E" + "02" + constant.REG_MAX_SEEK_HI + txtMAXH.Text
            + constant.REG_MAX_SEEK_LO + txtMAXL.Text + constant.REG_MIN_SEEK_HI + txtMINH.Text + constant.REG_MIN_SEEK_LO + txtMINL.Text
            + constant.ALERT_CURR_MAX_LIMIT_HI + txtCURH.Text + constant.ALERT_CURR_MAX_LIMIT_LO + txtCURL.Text);
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + scixml.Read_ServoID() + "0E" + "02" + constant.REG_MAX_SEEK_HI + txtMAXH.Text
            + constant.REG_MAX_SEEK_LO + txtMAXL.Text + constant.REG_MIN_SEEK_HI + txtMINH.Text + constant.REG_MIN_SEEK_LO + txtMINL.Text
            + constant.ALERT_CURR_MAX_LIMIT_HI + txtCURH.Text + constant.ALERT_CURR_MAX_LIMIT_LO + txtCURL.Text + sum_str;
            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
            Settings.Default.MaxPosiH = txtMAXH.Text;
            Settings.Default.MaxPosiL = txtMAXL.Text;
            Settings.Default.MinPosiH = txtMINH.Text;
            Settings.Default.MinPosiL = txtMINL.Text;
            Settings.Default.Save();
            this.Hide();
            this.Dispose();
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }
    }
}